package alfred.mdp.broker;

import alfred.mdp.Majordomo;
import alfred.mdp.utils.Shutdowner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 */
public class CheckWorkersTask implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(CheckWorkersTask.class);
    private final Broker broker;

    public CheckWorkersTask(Broker broker) {
        this.broker = broker;
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(Majordomo.CHECK_INTERVAL);
                broker.checkWorkers();

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();

            } catch (Exception e) {
                LOG.error("Error", e);

            }
        }
        Shutdowner.shutdownCompleted(broker.getShutdownGroup(), getClass());
        LOG.debug("shutdown completed");
    }
}
